﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using AMS.Core.Entities;
using AMS.History.DataAccess;
using AMS.History.Entities;
using AMS.WorkBill.DataAccess;
using AMS.WorkBill.Entities;
using VitCode.BaseDataPageCreator;
using VitCode.BaseDataPageCreator.Setting;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;

namespace AMS.Web.WorkBill
{
    public class WoSaveHelper
    {
        private PageBuilder m_PageBuilder;
        private PageSaveHelper m_PageSaveHelper;
        private WoRuleHelper m_RuleHelper;
        private Page m_Page;

        public PageBuilder CurrPageBuilder
        {
            get { return m_PageBuilder; }
        }

        public Page CurrPage
        {
            get { return m_Page; }
        }

        private string m_XmlSettingString;

        public string XmlSettingString
        {
            get { return m_XmlSettingString; }
        }

        public static string GET_FROM_OBJ_PRO = "GetFromObjPro", PRO_VALUE_INFO = "ProValueInfo",SESSION_OBJECT_HEAD_STRING="WoPageBuilder_";
        public static string VALUE_FROM_WO_OBJ_PRO = "ValueFromWoObjPro",EXEC_TO_OBJ_PRO = "ExecToObjPro",IS_NOT_NULL="IsNotNull";

        private object GetSessionObject(string strKey)
        {
            return m_Page.Session[SESSION_OBJECT_HEAD_STRING + strKey];
        }


        private void SetSessionObject(string strKey, object val)
        {
            m_Page.Session[SESSION_OBJECT_HEAD_STRING + strKey] = val;
        }


        public WoSaveHelper (Page page, string settingXmlPath)
        {
            m_Page = page;
            m_XmlSettingString = settingXmlPath;
            m_PageSaveHelper = new PageSaveHelper(m_Page, m_XmlSettingString);
            m_PageBuilder = new PageBuilder(m_Page, m_XmlSettingString);
            m_RuleHelper = new WoRuleHelper();
        }

        public WorkBillInfo GetCurrSessionWoInfo(Page currPage,string woInfoId)
        {
            object reVal = currPage.Session[WorkBillInfo.PAGE_SESSION_NAME];
            if (reVal == null)
            {
                SetCurrSessionWoInfo(currPage, woInfoId);
                reVal = currPage.Session[WorkBillInfo.PAGE_SESSION_NAME];
            }
            if (reVal != null && reVal is WorkBillInfo)
                return (WorkBillInfo)reVal;
            return null;
        }

        public WorkBillInfo GetCurrSessionWoInfo(Page currPage)
        {
            return GetCurrSessionWoInfo(currPage, "");
        }

        public void SetCurrSessionWoInfo(Page currPage,string woInfoId)
        {
            if (woInfoId.Length == 36)
            {
                WorkBillAccess acc = new WorkBillAccess();
                try
                {
                    object objVal = acc.GetSingleObjectById(typeof(WorkBillInfo).Name, woInfoId);
                    if (objVal != null && objVal is WorkBillInfo)
                        currPage.Session[WorkBillInfo.PAGE_SESSION_NAME] = objVal;
                }
                finally
                {
                    acc.CloseSession();
                }
            }
        }

        public void SetCurrSessionWoInfoByCode(Page currPage, string woCode)
        {
            if (woCode.Length == 14)
            {
                WorkBillAccess acc = new WorkBillAccess();
                try
                {
                    object objVal = acc.GetSingleObjectByProVal(typeof(WorkBillInfo).Name,"Code", woCode);
                    if (objVal != null && objVal is WorkBillInfo)
                        currPage.Session[WorkBillInfo.PAGE_SESSION_NAME] = objVal;
                }
                finally
                {
                    acc.CloseSession();
                }
            }
        }

        public void SetCurrSessionWoInfo(Page currPage,WorkBillInfo woInfo)
        {
            currPage.Session[WorkBillInfo.PAGE_SESSION_NAME] = woInfo;
        }

        public void ClearCurrWoInfo(Page currPage)
        {
            currPage.Session[WorkBillInfo.PAGE_SESSION_NAME] = null;
        }

        private string CreateProHqlCondition(string strProValInfo)
        {
            var sb = new StringBuilder();
            
            if(!string.IsNullOrEmpty(strProValInfo))
            {
                string[] ss = strProValInfo.Split(',');
                foreach(string str in ss)
                {
                    string[] keyVal = str.Split(':');
                    if(keyVal.Length == 2 )
                    {
                        if (sb.Length > 0)
                            sb.Append(" and ");
                        sb.Append("(o."+keyVal[0]+"='"+keyVal[1]+"')");
                    }
                }
            }
            return sb.ToString();
        }

        private void SetInitCommandPro(WoCommand comm,XmlNode proNode,BaseDataAccess acc)
        {
            if (comm == null || proNode == null || acc == null)
                return;
            string strName = m_PageSaveHelper.GetNodeAttributeValue(proNode, m_PageBuilder.m_LoadHelper.NAME,false);
            try
            {
                if (proNode.Attributes.Count == 0)
                    return;
                string strGetFromObjPro = m_PageSaveHelper.GetNodeAttributeValue(proNode, GET_FROM_OBJ_PRO, true);
                string strProValueInfo = m_PageSaveHelper.GetNodeAttributeValue(proNode, PRO_VALUE_INFO, true);
                string strClassName = m_PageSaveHelper.GetNodeAttributeValue(proNode, m_PageBuilder.m_LoadHelper.CLASS_NAME, true);
                string strValueFromWoObjPro = m_PageSaveHelper.GetNodeAttributeValue(proNode, VALUE_FROM_WO_OBJ_PRO,
                                                                                     true);
                PropertyInfo commPi = comm.GetType().GetProperty(strName);
                if (commPi == null)
                    return;

                if(strGetFromObjPro.Length > 0)
                {
                    string[] ss = strGetFromObjPro.Split('.');
                    if(ss.Length == 2)
                    {
                        PropertyInfo pi = comm.GetType().GetProperty(ss[0]);
                        if(pi!=null)
                        {
                            object objPro = pi.GetValue(comm, null);
                            if(objPro!=null)
                            {
                                PropertyInfo pi2 = objPro.GetType().GetProperty(ss[1]);
                                if (pi2 != null)
                                {
                                    object objProVal2 = pi2.GetValue(objPro,null);
                                    commPi.SetValue(comm, objProVal2,null);
                                }
                            }
                        }
                    }
                }
                if(strProValueInfo.Length > 0 && strClassName.Length > 0)
                {
                    string strSessionKey = strName + "_" + strProValueInfo;
                    object objVal = GetSessionObject(strSessionKey);
                    if (objVal == null)
                    {
                        string strHqlCondition = CreateProHqlCondition(strProValueInfo);
                        if(strHqlCondition.Length==0)
                            return;
                        IList lstObjs = acc.Session.CreateQuery("From "+strClassName+" o where " + strHqlCondition).List();
                        if (lstObjs != null && lstObjs.Count > 0)
                            SetSessionObject(strSessionKey, lstObjs[0]);
                    }

                    objVal = GetSessionObject(strSessionKey);
                    commPi.SetValue(comm, objVal, null);
                }

                if(strValueFromWoObjPro.Length > 0)
                {
                    object woInfo = GetCurrSessionWoInfo(m_Page);
                    if (woInfo == null)
                        throw new ArgumentException("Session中的WoInfo对象不能为空");
                    PropertyInfo pi = woInfo.GetType().GetProperty(strValueFromWoObjPro);
                    if(pi!=null)
                    {
                        object objProVal = pi.GetValue(woInfo, null);
                        if (objProVal != null)
                            commPi.SetValue(comm, objProVal, null);
                    }
                }
                
            }
            catch (Exception ex)
            {
                m_PageBuilder.m_LoadHelper.XmlReader.ShowErrorSettingInfo(proNode, ex);
            }
        }

        public void SetInitCommandProSet(WoCommand comm,string className,string typeName)
        {
            XmlNodeList nl = m_PageBuilder.m_LoadHelper.XmlReader.GetNodeList("Setting/" + className + "/" + typeName +
                                                             "/commandSetting/CommandPros/" + WoPageBuilder.INIT+ "/property");
            BaseDataAccess acc = new BaseDataAccess();
            foreach (XmlNode n in nl)
            {
                SetInitCommandPro(comm, n, acc);
            }
        }

        public void CreateWoCommand(string strIds, WorkBillInfo wo,string typeName, WorkBillAccess acc)
        {
            CreateWoCommand(ConvertToStringCollection(strIds), wo, typeName, acc);
        }

        public void CreateWoCommand(StringCollection scIds, WorkBillInfo wo, string typeName, WorkBillAccess acc)
        {
            if (scIds != null && scIds.Count > 0)
            {
                NameObjectDictionary ndAssetInfoBarcodeSet = acc.GetObjectsByKeyValues(typeof(AssetInfo).Name, "Id", scIds);
                AssetState engrossAssetState = acc.GetEngrossAssetState();
                if (ndAssetInfoBarcodeSet != null && ndAssetInfoBarcodeSet.Count > 0)
                {
                    acc.Session.SaveOrUpdate(wo);
                    foreach (AssetInfo obj in ndAssetInfoBarcodeSet.Values)
                    {
                        WoCommand comm = new WoCommand();
                        comm.AssetInfo = obj;
                        comm.WoCode = wo.Code;
                        comm.Flag = WoCommand.STATE_SELECTED;
                        SetInitCommandProSet(comm, wo.GetType().Name, typeName);
                        obj.State = engrossAssetState;
                        acc.Session.SaveOrUpdate(obj);
                        acc.Session.SaveOrUpdate(comm);
                    }
                }
            }
        }

        public StringCollection ConvertToStringCollection(string strIds)
        {
            if (string.IsNullOrEmpty(strIds))
                return null;
            StringCollection scIds = new StringCollection();
            string[] ssIds = strIds.Split(',');
            foreach (string strId in ssIds)
            {
                if (strId.Length == 36)
                {
                    if (!scIds.Contains(strId))
                        scIds.Add(strId);
                }
            }
            return scIds;
        }

        public string SetObjectPropetyValueWithRule(WoCommand comm, string woTypeName, XmlNode proNode, BaseDataAccess acc, UserControl uc,NameObjectDictionary ndChildObjs)
        {
            string strErrMsg = "";
            try
            {
                if (proNode.Attributes.Count == 0)
                    return strErrMsg;
                string strLgnoreSave = CurrPageBuilder.GetNodeAttributeValue(proNode, CurrPageBuilder.m_LoadHelper.LGNORE_SAVE, true);
                string strClassName = CurrPageBuilder.GetNodeAttributeValue(proNode, CurrPageBuilder.m_LoadHelper.CLASS_NAME, true);
                string strCtlObjKeyPro = CurrPageBuilder.GetNodeAttributeValue(proNode, CurrPageBuilder.m_LoadHelper.CTL_OBJ_KEY_PAO, true);
                if (strLgnoreSave.ToLower() == "true")
                    return strErrMsg;
                string strName = CurrPageBuilder.m_LoadHelper.OBJ_PRO_HEAD_STRING +
                                 CurrPageBuilder.GetNodeAttributeValue(proNode, CurrPageBuilder.m_LoadHelper.NAME, false);
                PropertyInfo pi = CurrPageBuilder.m_LoadHelper.GetProperty(proNode, comm.GetType());
                Control ctl;
                object proValue = null;
                if (uc == null)
                    ctl = m_Page.FindControl(strName);
                else
                    ctl = uc.FindControl(strName);
                if (ctl == null)
                    return strErrMsg;

                if (strClassName.Length > 0)
                {
                    if (strCtlObjKeyPro.Length == 0)
                    {
                        throw new ArgumentException("在设置ClassName的同时,请设置CtlObjKeyPro属性");
                    }
                    string strProObjKeyProVal = CurrPageBuilder.m_LoadHelper.GetProObjStringValue(strCtlObjKeyPro, ctl);
                    string strProObjKyeProName = CurrPageBuilder.m_LoadHelper.GetClassKeyPropertyName(strClassName);
                    if (ndChildObjs == null) 
                        ndChildObjs = new NameObjectDictionary();
                    if (ndChildObjs.Contains(strName))
                    {
                        proValue = ndChildObjs[strName];
                    }
                    else
                    {
                        proValue = m_PageBuilder.GetObjectByHql(strClassName, strProObjKyeProName, strProObjKeyProVal,
                                                            acc.Session);
                        ndChildObjs.Add(strName, proValue);
                    }                                       
                    if (pi.CanWrite)
                        pi.SetValue(comm, proValue, null);
                    return strErrMsg;
                }
                if (ctl.GetType() == typeof(TextBox))
                {
                    string strText = ((TextBox)ctl).Text;
                    if (pi.PropertyType == typeof(DateTime))
                    {
                        proValue = Convert.ToDateTime(strText);
                    }
                    else if (pi.PropertyType == typeof(Int32) || pi.PropertyType == typeof(Double) || pi.PropertyType == typeof(float))
                    {
                        string strVal = ((TextBox)ctl).Text;
                        if (strVal.Length > 0)
                            proValue = Convert.ChangeType(strVal, pi.PropertyType);
                    }
                    else
                        proValue = ((TextBox)ctl).Text;
                }
                else if (ctl.GetType() == typeof(CheckBox))
                    proValue = ((CheckBox)ctl).Checked;
                else
                {
                    string ctlProNameForObj = CurrPageBuilder.m_LoadHelper.XmlReader.GetNodeAttributeValue(proNode,
                                                                                                           CurrPageBuilder.m_LoadHelper.CTL_PROPERTY_FRO_OBJCET,
                                                                                                           true);
                    if (ctlProNameForObj.Length == 0)
                        ctlProNameForObj = "Text";
                    PropertyInfo piKeyHeadString = ctl.GetType().GetProperty("KeyHeadString");
                    if (piKeyHeadString != null)
                    {
                      
                            PropertyInfo piObjCode = comm.GetType().GetProperty("Code");
                            if (piObjCode != null)
                                piKeyHeadString.SetValue(ctl, piObjCode.GetValue(comm, null), null);
                       
                    }
                    PropertyInfo ctlTextPro = ctl.GetType().GetProperty(ctlProNameForObj);

                    if (ctlTextPro != null)
                    {
                        proValue = ctlTextPro.GetValue(ctl, null);
                    }
                }
                if (proValue == null)
                    pi.SetValue(comm, null, null);
                else
                {
                    if (pi.PropertyType == typeof(Int32) && ! CreateControlHelper.CheckIsInt(proValue.ToString()))
                        return strErrMsg;
                    if (pi.CanWrite)
                    {
                        strErrMsg = m_RuleHelper.CheckWoCommandUpdate(comm, woTypeName, pi, proValue);
                        if(string.IsNullOrEmpty(strErrMsg))
                            pi.SetValue(comm, Convert.ChangeType(proValue, pi.PropertyType), null);
                    }
                }
            }
            catch (Exception ex)
            {
                CurrPageBuilder.m_LoadHelper.XmlReader.ShowErrorSettingInfo(proNode, ex);
            }
            return strErrMsg;
        }

        public void UpdateWoCommand(StringCollection scIds, Type commType, string woClassName, string woTypeName, WorkBillAccess acc, UserControl uc, ref StringCollection scErrInfo)
        {
            if (scIds != null && scIds.Count > 0)
            {
                NameObjectDictionary ndCommSet = acc.GetObjectsByKeyValues(typeof(WoCommand).Name, "Id", scIds);
                UpdateWoCommand(ndCommSet, commType, woClassName, woTypeName, acc, uc, ref scErrInfo);
            }
        }

        public void UpdateWoCommand(string woCode, Type commType, string woClassName, string woTypeName, WorkBillAccess acc, UserControl uc, ref StringCollection scErrInfo)
        {
            if (!string.IsNullOrEmpty(woCode))
            {
                NameObjectDictionary ndCommSet = acc.GetObjectsByKeyValues(typeof(WoCommand).Name, "Id", "WoCode", new StringCollection { woCode });
                UpdateWoCommand(ndCommSet, commType, woClassName, woTypeName, acc, uc, ref scErrInfo);
            }
        }


        public void UpdateWoCommand(NameObjectDictionary ndCommSet, Type commType, string woClassName, string woTypeName, WorkBillAccess acc, UserControl uc, ref StringCollection scErrInfo)
        {

            if (ndCommSet != null && ndCommSet.Count > 0)
            {
                XmlNodeList proNodeList = m_PageBuilder.m_LoadHelper.XmlReader.GetNodeList("Setting/" + woClassName + "/" + woTypeName +
                                                             "/commandSetting/CommandPros/" + WoPageBuilder.EDIT + "/property");
                AssetState asUsed = acc.GetUsedAssetState();
                AssetState asIdleness = acc.GetIdlenessAssetState();
                int amount = 0;
                NameObjectDictionary ndChildObj = new NameObjectDictionary();
                if (ndCommSet != null && ndCommSet.Count > 0)
                {
                    foreach (WoCommand obj in ndCommSet.Values)
                    {
                        amount++;
                        string strErrMsg = "";
                        foreach (XmlNode proNode in proNodeList)
                        {
                            strErrMsg = SetObjectPropetyValueWithRule(obj, woTypeName, proNode, acc, uc, ndChildObj);
                        }
                        obj.Flag = WoCommand.STATE_SET;
                        if (obj.TargetUser != null && obj.TargetUserGroup == null && obj.TargetUser.UserGroup != null)
                            obj.TargetUserGroup = obj.TargetUser.UserGroup;
                        if (obj.TargetSite != null && obj.TargetUserGroup == null && obj.TargetSite.Department != null)
                            obj.TargetUserGroup = obj.TargetSite.Department;
                        if (obj.TargetState == null && obj.TargetSite != null)
                        {
                            obj.TargetState = obj.TargetSite.TypeName.ToLower() == "warehouse" ? asIdleness: asUsed;
                        }
                        if (!string.IsNullOrEmpty(strErrMsg))
                        {
                            scErrInfo.Add(strErrMsg);
                            continue;
                        }
                        acc.SaveOrUpdate(obj);
                    }
                }
            }
        }


        public void ExecToObjProValue(XmlNode proNode, WoCommand comm, WorkBillAccess acc, out string strErr,MonitorObjects hisMoObj)
        {
            strErr = "";
            ObjXmlReader xmlReader = m_PageBuilder.m_LoadHelper.XmlReader;
            PageLoadHelper loadHelper = m_PageBuilder.m_LoadHelper;
            string strExecToObjPro = xmlReader.GetNodeAttributeValue(proNode, EXEC_TO_OBJ_PRO, true);
            string strName = xmlReader.GetNodeAttributeValue(proNode, m_PageBuilder.m_LoadHelper.NAME, false);
            string strIsNotNull = xmlReader.GetNodeAttributeValue(proNode, IS_NOT_NULL, true);
            string strMemo = xmlReader.GetNodeAttributeValue(proNode, loadHelper.MEMO, true);

            if (strExecToObjPro.Length == 0 || comm == null)
                return;
            string[] ss = strExecToObjPro.Split('.');
            PropertyInfo piVal = comm.GetType().GetProperty(strName);
            if (piVal == null)
                return;
            object targetVal = piVal.GetValue(comm, null);
            if(ss!=null&&ss.Length==2)
            {
                string childObjProName = ss[0];
                string pro2Name = ss[1];
                if(childObjProName.Length>0)
                {
                    PropertyInfo childObjProPi = comm.GetType().GetProperty(childObjProName);
                    if(childObjProPi!= null)
                    {
                        object childObj = childObjProPi.GetValue(comm, null);
                        if(childObj!=null && childObj is AssetInfo)
                        {
                            AssetInfo ai = ((AssetInfo) childObj);
                            PropertyInfo childObjSetPro = ai.GetType().GetProperty(pro2Name);
                            if (childObjSetPro != null)
                            {
                                if (targetVal == null && strIsNotNull == "true")
                                {
                                    string assetInfoMemo = xmlReader.GetNodeAttributeValue(ai.GetType().Name, loadHelper.MEMO, true);
                                    strErr += "工单所带"+assetInfoMemo + "[" + ai.Barcode + "]，" + strMemo+"不能为空<br>";
                                }
                                else
                                {
                                 
                                    childObjSetPro.SetValue(ai, targetVal, null);
                                    
                                    acc.Session.Update(ai);
                                    hisMoObj.Listen(ai);
                                }
                            }
                        }
                    }
                }
            }
        }
        

        public void ExceWoCommand(WorkBillInfo woVal, string woClassName, string woTypeName, WorkBillAccess acc, out StringCollection scErr)
        {
            string strWoMemo = CurrPageBuilder.m_LoadHelper.XmlReader.GetNodeAttributeValue(woClassName, woTypeName,
                                                                                            CurrPageBuilder.m_LoadHelper
                                                                                                .MEMO,true);
            HistoryAccess hisAccess = new HistoryAccess();
            hisAccess.Session = acc.Session;
            var moObjs = new MonitorObjects();
            var hisVersion = new HisVersionInfo();
            hisVersion.Memo = strWoMemo;
            hisVersion.OperatorUserId = GenericContext.CurrUserId;
            XmlNodeList proNodeList = m_PageBuilder.m_LoadHelper.XmlReader.GetNodeList("Setting/" + woClassName + "/" + woTypeName +
                                                             "/commandSetting/CommandPros/" + WoPageBuilder.EXEC + "/property");
            scErr = new StringCollection();
            if (woVal!=null && woVal.State != WorkBillInfo.STATE_CLOSE)
            {
                NameObjectDictionary ndCommSet = acc.GetObjectsByKeyValues(typeof(WoCommand).Name,"Id", "WoCode", new StringCollection{woVal.Code});

                if (ndCommSet != null && ndCommSet.Count > 0)
                {
                    foreach (WoCommand obj in ndCommSet.Values)
                    {
                        foreach (XmlNode proNode in proNodeList)
                        {
                            string strErr = "";
                            ExecToObjProValue(proNode, obj, acc, out strErr, moObjs);
                            if (strErr.Length > 0)
                                scErr.Add(strErr);
                        }
                        obj.Flag = WoCommand.STATE_EXEC;
                        acc.SaveOrUpdate(obj);
                    }
                }
                else
                    throw new ArgumentException("请选择工单所带资产");
                woVal.State = WorkBillInfo.STATE_CLOSE;
                woVal.CloseDate = DateTime.Now;
                acc.SaveOrUpdate(woVal);
                hisVersion.WoInfoId = woVal.Id;
                hisAccess.SaveMonitorObjs(moObjs, typeof (AssetInfo), typeof (AssetInfoRecord), "Id", hisVersion);
            }
        }
        public string GetUpdateField(string className, string typeName, Control uc)
        {
            var comm = new WoCommand();
            var acc = new WorkBillAccess();
            var sql = "update AmsWorkBill_WoCommand set ";
            var proNodeList = m_PageBuilder.m_LoadHelper.XmlReader.GetNodeList("Setting/" + className + "/" + typeName +
                                                             "/commandSetting/CommandPros/" + WoPageBuilder.EDIT + "/property");

            if (proNodeList != null && proNodeList.Count > 0)
            {
                foreach (XmlNode proNode in proNodeList)
                {
                    if (proNode.Attributes.Count == 0)
                        return "";

                    string strClassName = CurrPageBuilder.GetNodeAttributeValue(proNode, CurrPageBuilder.m_LoadHelper.CLASS_NAME, true);
                    string strCtlObjKeyPro = CurrPageBuilder.GetNodeAttributeValue(proNode, CurrPageBuilder.m_LoadHelper.CTL_OBJ_KEY_PAO, true);
                    string strName = CurrPageBuilder.m_LoadHelper.OBJ_PRO_HEAD_STRING +
                             CurrPageBuilder.GetNodeAttributeValue(proNode, CurrPageBuilder.m_LoadHelper.NAME, false);
                    PropertyInfo pi = CurrPageBuilder.m_LoadHelper.GetProperty(proNode, comm.GetType());
                    Control ctl;
                    object proValue = null;
                    if (uc == null)
                        ctl = m_Page.FindControl(strName);
                    else
                        ctl = uc.FindControl(strName);
                    if (ctl == null)
                        return "";
                   
                    if (strClassName.Length > 0)
                    {
                        if (strCtlObjKeyPro.Length == 0)
                        {
                            throw new ArgumentException("在设置ClassName的同时,请设置CtlObjKeyPro属性");
                        }
                        string strProObjKeyProVal = CurrPageBuilder.m_LoadHelper.GetProObjStringValue(strCtlObjKeyPro, ctl);
                        string strProObjKyeProName = CurrPageBuilder.m_LoadHelper.GetClassKeyPropertyName(strClassName);
                        proValue = m_PageBuilder.GetObjectByHql(strClassName, strProObjKyeProName, strProObjKeyProVal,
                                                          acc.Session);
                        if (strProObjKyeProName == "Id")
                        {
                            sql += pi.Name + strProObjKyeProName + " = '" + strProObjKeyProVal + "',";
                        }
                        else
                        {
                            sql += pi.Name + " = '" + proValue + "',";
                        }

                    }
                    else
                    {
                        if (ctl.GetType() == typeof(TextBox))
                        {
                            string strText = ((TextBox)ctl).Text;
                            if (pi.PropertyType == typeof(DateTime))
                            {
                                proValue = Convert.ToDateTime(strText);
                            }
                            else if (pi.PropertyType == typeof(Int32) || pi.PropertyType == typeof(Double) || pi.PropertyType == typeof(float))
                            {
                                string strVal = ((TextBox)ctl).Text;
                                if (strVal.Length > 0)
                                    proValue = Convert.ChangeType(strVal, pi.PropertyType);
                            }
                            else
                                proValue = ((TextBox)ctl).Text;
                            sql += pi.Name + " = '" + proValue + "',";
                        }
                        else if (ctl.GetType() == typeof(CheckBox))
                        {
                            proValue = ((CheckBox)ctl).Checked;
                            sql += pi.Name + " = '" + proValue + "',";
                        }

                        else
                        {
                            string ctlProNameForObj = CurrPageBuilder.m_LoadHelper.XmlReader.GetNodeAttributeValue(proNode,
                                                                                                                   CurrPageBuilder.m_LoadHelper.CTL_PROPERTY_FRO_OBJCET,
                                                                                                                   true);
                            if (ctlProNameForObj.Length == 0)
                                ctlProNameForObj = "Text";
                            PropertyInfo piKeyHeadString = ctl.GetType().GetProperty("KeyHeadString");
                            if (piKeyHeadString != null)
                            {

                                PropertyInfo piObjCode = comm.GetType().GetProperty("Code");
                                if (piObjCode != null)
                                    piKeyHeadString.SetValue(ctl, piObjCode.GetValue(comm, null), null);

                            }
                            var ctlTextPro = ctl.GetType().GetProperty(ctlProNameForObj);

                            if (ctlTextPro != null)
                            {
                                proValue = ctlTextPro.GetValue(ctl, null);
                            }
                            if (proValue == null)
                                sql += pi.Name + " = 'NULL'";
                            else
                            {
                                if (pi.PropertyType == typeof(Int32) && !CreateControlHelper.CheckIsInt(proValue.ToString()))
                                    return "";
                                if (pi.CanWrite)
                                {
                                    sql += pi.Name + " = '" + proValue + "',";
                                    pi.SetValue(comm, Convert.ChangeType(proValue, pi.PropertyType), null);
                                }
                            }
                        }
                    }

                }
            }
            return sql;
        }
    }
}
